package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.device.phone.entity.PhoneConstant;
import com.csun.cmny.provider.model.domain.DmcPhoneZone;
import com.csun.cmny.provider.model.domain.DmcPhoneZoneType;
import com.csun.cmny.provider.model.dto.phone.DmcCheckPhoneZoneNoDto;
import com.csun.cmny.provider.model.dto.phone.DmcEditPhoneZoneDto;
import com.csun.cmny.provider.model.dto.phone.PhoneZoneBindDto;
import com.csun.cmny.provider.model.vo.DmcPhoneZoneVo;
import com.csun.cmny.provider.model.vo.OldmanBedRoomBuildingVo;
import com.csun.cmny.provider.model.vo.RoomBuildingVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcPhoneZoneService;
import com.csun.cmny.provider.service.DmcPhoneZoneTypeService;
import com.csun.cmny.provider.validator.DmcPhoneZoneAddValidator;
import com.csun.cmny.provider.validator.DmcPhoneZoneEditValidator;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-08 13:47
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping(value = "/phone", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcPhoneZoneController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcPhoneZoneTypeService phoneZoneTypeService;
    @Resource
    private DmcPhoneZoneService phoneZoneService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 校验指定电话机设备的防区编号是否存在
     *
     * @param dmcCheckPhoneZoneNoDto    the dmc check phone zone numer dto
     * @param br                        binding result
     *
     * @return the wrapper
     */
    @RequestMapping(value = "/checkPhoneZoneNo", method = RequestMethod.POST)
    public Wrapper<Boolean> checkPhoneZoneNo(@Validated @RequestBody DmcCheckPhoneZoneNoDto dmcCheckPhoneZoneNoDto,
                                             BindingResult br) {

        Example example = new Example(DmcPhoneZone.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo("deviceId", dmcCheckPhoneZoneNoDto.getDeviceId());
        criteria.andEqualTo("zoneNo", dmcCheckPhoneZoneNoDto.getZoneNo());

        Integer result = phoneZoneService.selectCountByExample(example);

      //  return WrapMapper.ok(result < 1);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result<1);
    }

    /**
     * 查询防区类型
     *
     * @return the phone zone type list.
     */
    @RequestMapping(value = "/queryPhoneZoneTypeList", method = RequestMethod.POST)
    public Wrapper queryPhoneZoneTypeList() {

        List<DmcPhoneZoneType> dmcPhoneZoneType = phoneZoneTypeService.queryPhoneZoneTypeList();

       // return WrapMapper.ok(dmcPhoneZoneType);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),dmcPhoneZoneType);
    }

    /**
     * 查询电话机的防区列表
     *
     * @param deviceId the device id.
     *
     * @return the device phone zone list
     */
    @RequestMapping(value = "/queryPhoneZoneList/{deviceId}", method = RequestMethod.POST)
    public Wrapper queryPhoneZoneList(@PathVariable Long deviceId) {

        List<DmcPhoneZoneVo> dmcPhoneZoneVoList = phoneZoneService.queryPhoneZoneList(deviceId);
        /**
         * @date    2019-11-28
         * @author  chisj
         * @desc    防区绑定了需要展示绑定的位置或者老人
         */
        for (DmcPhoneZoneVo z : dmcPhoneZoneVoList) {
            if (z.getBind()) {
                OldmanBedRoomBuildingVo obrbv = null;
                switch (z.getBindType()) {
                    case PhoneConstant.PHONE_ZONE_BIND_TYPE_CUSTOM :
                        // 自定义
                        break;
                    case PhoneConstant.PHONE_ZONE_BIND_TYPE_OLDMAN :
                        // 绑定老人
                        obrbv = (OldmanBedRoomBuildingVo) redisTemplate.opsForValue().get(RedisKeyUtil.getOmcOldmanId(z.getOldmanId()));
                        if (PublicUtil.isNotEmpty(obrbv)) {
                            z.setOldmanName(obrbv.getOldmanName());
                            if (PublicUtil.isNotEmpty(obrbv.getRoomNo()) && PublicUtil.isNotEmpty(obrbv.getBedNo())) {
                                z.setLocation(obrbv.getRoomNo() + "-" + obrbv.getBedNo());
                        }
                        } else {
                            log.error("电话机防区绑定的老人不存在.");
                        }
                        break;
                    case PhoneConstant.PHONE_ZONE_BIND_TYPE_BED :
                        obrbv = (OldmanBedRoomBuildingVo) redisTemplate.opsForValue().get(RedisKeyUtil.getOmcBedId(z.getBedId()));
                        if (PublicUtil.isNotEmpty(obrbv)) {
                            z.setOldmanId(obrbv.getOldmanId());
                            z.setOldmanName(obrbv.getOldmanName());
                            if (PublicUtil.isNotEmpty(obrbv.getRoomNo()) && PublicUtil.isNotEmpty(obrbv.getBedNo())) {
                                z.setLocation(obrbv.getRoomNo() + "-" + obrbv.getBedNo());
                            }
                        } else {
                            // 报警器-绑定床位 床位不存在
                            log.error("电话机防区绑定的床位不存在.");
                        }
                        break;
                    case PhoneConstant.PHONE_ZONE_BIND_TYPE_ROOM :
                        RoomBuildingVo rbv = (RoomBuildingVo) redisTemplate
                                .opsForValue().get(RedisKeyUtil.getOmcRoomId(z.getRoomId()));
                        if (PublicUtil.isNotEmpty(rbv)) {
                            z.setLocation(rbv.getLocation());
                        }
                        break;
                }
            }
        }

       // return WrapMapper.ok(dmcPhoneZoneVoList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),dmcPhoneZoneVoList);
    }

    /**
     * 根据Id删除防区
     *
     * @param id the id
     *
     * @return the wrapper
     */
    @RequestMapping(value = "/deletePhoneZoneById/{id}", method = RequestMethod.POST)
    public Wrapper deletePhoneZoneById(@PathVariable Long id) {

        Integer result = phoneZoneService.deletePhoneZoneById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 根据deviceId删除防区
     *
     * @param deviceId the id
     *
     * @return the wrapper
     */
    @RequestMapping(value = "/deletePhoneZoneByDeviceId/{deviceId}", method = RequestMethod.POST)
    public Wrapper deletePhoneZoneByDeviceId(@PathVariable Long deviceId) {

        Integer result = phoneZoneService.deletePhoneZoneByDeviceId(deviceId);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 添加电话机防区
     *
     * @param dmcEditPhoneZoneDto the dmc edit phone zone dto
     * @param br the binding result
     *
     * @return the wrapper
     */
    @RequestMapping(value = "/addPhoneZone", method = RequestMethod.POST)
    public Wrapper addPhoneZone(@Validated(value = {DmcPhoneZoneAddValidator.class})
                                @RequestBody DmcEditPhoneZoneDto dmcEditPhoneZoneDto, BindingResult br) {

        DmcPhoneZone dmcPhoneZone = new DmcPhoneZone();

        BeanUtils.copyProperties(dmcEditPhoneZoneDto, dmcPhoneZone);

        Integer result = phoneZoneService.addPhoneZone(dmcPhoneZone, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 修改电话机防区
     *
     * @param dmcEditPhoneZoneDto the dmc edit phone zone dto
     * @param br the binding result
     *
     * @return the wrapper
     */
    @RequestMapping(value = "/editPhoneZone", method = RequestMethod.POST)
    public Wrapper editPhoneZone(@Validated(value = {DmcPhoneZoneEditValidator.class})
                                 @RequestBody DmcEditPhoneZoneDto dmcEditPhoneZoneDto, BindingResult br) {

        DmcPhoneZone dmcPhoneZone = new DmcPhoneZone();

        BeanUtils.copyProperties(dmcEditPhoneZoneDto, dmcPhoneZone);

        Integer result = phoneZoneService.editPhoneZone(dmcPhoneZone, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return  WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 防区绑定
     */
    @ValidateAnnotation
    @PostMapping(value = "/zoneBind")
    public Wrapper zoneBind(@Validated @RequestBody PhoneZoneBindDto phoneZoneBindDto, BindingResult br) {

        Integer result = phoneZoneService.bind(phoneZoneBindDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * 防区解绑
     */
    @PostMapping(value = "/zoneUntied/{id}")
    public Wrapper zoneUntied(@PathVariable Long id) {

        Integer result = phoneZoneService.zoneUntied(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

       // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }
}
